{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "## Pytorch\n",
    "\n",
    "PyTorch是一个动态的建图的工具。不像Tensorflow那样，先建图，然后通过feed和run重复执行建好的图。相对来说，PyTorch具有更好的灵活性。\n",
    "\n",
    "\n",
    "### 如何保存参数\n",
    "\n",
    "pytorch中最重要的data type。\n",
    "\n",
    "**Tensor**： 就像ndarray一样,一维Tensor叫Vector，二维Tensor叫Matrix，三维及以上称为Tensor(张量)\n",
    "\n",
    "**Tensor**与**ndarray**的最主要区别：Tensor可以在GPU上进行计算，可以自动求导。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 1.3592e-19,  7.5553e+28,  5.2839e-11,  1.8888e+31],\n",
       "         [ 9.8049e-09,  1.6037e-07,  1.3817e-19,  4.4721e+21],\n",
       "         [ 6.2625e+22,  4.7428e+30,  2.6800e+20,  1.4348e-19]],\n",
       "\n",
       "        [[-9.1812e+22, -3.4566e-34, -1.6980e-38, -4.0311e+23],\n",
       "         [-1.3823e-33, -6.4672e-32, -1.4219e+24,  6.2724e+22],\n",
       "         [ 4.7428e+30,  3.3669e-18,  1.8590e+34,  7.7767e+31]]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x  = torch.Tensor(2,3,4) # torch.Tensor(shape) 创建出一个未初始化的Tensor\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([2, 3, 4])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.size()\n",
    "# x.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[0.9333, 0.9679, 1.0367, 0.9379],\n",
      "         [0.6609, 0.3134, 1.5635, 1.3560],\n",
      "         [1.1026, 0.5994, 0.9452, 0.5979]],\n",
      "\n",
      "        [[1.2640, 0.0791, 1.0301, 0.9093],\n",
      "         [0.8819, 0.3511, 1.0857, 0.6958],\n",
      "         [0.7245, 0.4092, 0.8632, 1.3441]]])\n"
     ]
    }
   ],
   "source": [
    "a = torch.rand(2,3,4)\n",
    "b = torch.rand(2,3,4)\n",
    "\n",
    "x = torch.add(a,b)  # 使用Tensor()方法创建出来的Tensor用来接收计算结果，当然torch.add(..)也会返回计算结果的\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.9333, 0.9679, 1.0367, 0.9379],\n",
       "         [0.6609, 0.3134, 1.5635, 1.3560],\n",
       "         [1.1026, 0.5994, 0.9452, 0.5979]],\n",
       "\n",
       "        [[1.2640, 0.0791, 1.0301, 0.9093],\n",
       "         [0.8819, 0.3511, 1.0857, 0.6958],\n",
       "         [0.7245, 0.4092, 0.8632, 1.3441]]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0.5463, 0.0488, 0.4967, 0.7042],\n",
       "         [0.4067, 0.8350, 0.9757, 0.1101],\n",
       "         [0.9653, 0.6296, 0.8876, 0.4811]],\n",
       "\n",
       "        [[0.1561, 0.8859, 0.9208, 0.5756],\n",
       "         [0.8219, 0.4491, 0.5427, 0.2016],\n",
       "         [0.0949, 0.4948, 0.8772, 0.1067]]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1.2818, 1.3501, 1.7361, 0.8297],\n",
       "         [1.5176, 0.2186, 0.6820, 0.6451],\n",
       "         [1.6785, 0.4612, 1.1057, 1.7063]],\n",
       "\n",
       "        [[1.6335, 1.0912, 0.5792, 0.8746],\n",
       "         [0.8269, 1.1090, 0.9925, 0.8852],\n",
       "         [1.6566, 0.5550, 0.7133, 0.9053]]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.add_(b) # 所有带 _ 的operation，都会更改调用对象的值 a = a + b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[1.2818, 1.3501, 1.7361, 0.8297],\n",
       "         [1.5176, 0.2186, 0.6820, 0.6451],\n",
       "         [1.6785, 0.4612, 1.1057, 1.7063]],\n",
       "\n",
       "        [[1.6335, 1.0912, 0.5792, 0.8746],\n",
       "         [0.8269, 1.1090, 0.9925, 0.8852],\n",
       "         [1.6566, 0.5550, 0.7133, 0.9053]]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 自动求导\n",
    "\n",
    "pytorch的自动求导工具包在torch.autograd中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([4.])\n",
      "tensor([2.])\n"
     ]
    }
   ],
   "source": [
    "x = torch.tensor([2.0], requires_grad = True)\n",
    "a = torch.tensor([4.0], requires_grad = True)\n",
    "y = x * a\n",
    "\n",
    "y.backward()    \n",
    "\n",
    "print(x.grad)   # 2 * x = 4\n",
    "print(a.grad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "x_train = torch.rand(100)\n",
    "y_train = x_train * 2 + 3 # w = 2, b = 3, y = 2 * x + 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'torch.Tensor'>\n"
     ]
    }
   ],
   "source": [
    "w = torch.tensor([0.0],requires_grad = True)\n",
    "b = torch.tensor([0.0],requires_grad = True)\n",
    "\n",
    "print(type(w))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n",
       "        0., 0., 0., 0.], grad_fn=<AddBackward0>)"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pre = x_train * w + b\n",
    "y_pre"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Iter: 0, w: 0.0000, b: 0.0000, training loss: 16.3612\n",
      "Iter: 10, w: 0.5530, b: 1.0139, training loss: 7.5307\n",
      "Iter: 20, w: 0.9299, b: 1.7006, training loss: 3.4682\n",
      "Iter: 30, w: 1.1873, b: 2.1654, training loss: 1.5991\n",
      "Iter: 40, w: 1.3637, b: 2.4797, training loss: 0.7392\n",
      "Iter: 50, w: 1.4850, b: 2.6919, training loss: 0.3434\n",
      "Iter: 60, w: 1.5690, b: 2.8349, training loss: 0.1612\n",
      "Iter: 70, w: 1.6277, b: 2.9311, training loss: 0.0773\n",
      "Iter: 80, w: 1.6691, b: 2.9954, training loss: 0.0385\n",
      "Iter: 90, w: 1.6987, b: 3.0382, training loss: 0.0206\n",
      "Iter: 100, w: 1.7204, b: 3.0663, training loss: 0.0122\n",
      "Iter: 110, w: 1.7367, b: 3.0846, training loss: 0.0083\n",
      "Iter: 120, w: 1.7492, b: 3.0962, training loss: 0.0063\n",
      "Iter: 130, w: 1.7591, b: 3.1033, training loss: 0.0053\n",
      "Iter: 140, w: 1.7673, b: 3.1073, training loss: 0.0048\n",
      "Iter: 150, w: 1.7743, b: 3.1093, training loss: 0.0044\n",
      "Iter: 160, w: 1.7804, b: 3.1099, training loss: 0.0042\n",
      "Iter: 170, w: 1.7859, b: 3.1096, training loss: 0.0040\n",
      "Iter: 180, w: 1.7909, b: 3.1087, training loss: 0.0038\n",
      "Iter: 190, w: 1.7956, b: 3.1073, training loss: 0.0036\n"
     ]
    }
   ],
   "source": [
    "lr = 0.015\n",
    "loss_func = torch.nn.MSELoss()\n",
    "for i in range(200):\n",
    "    y_pre = x_train * w + b\n",
    "\n",
    "    loss = loss_func(y_train, y_pre)\n",
    "    if i % 10 == 0:\n",
    "        print(\"Iter: %d, w: %.4f, b: %.4f, training loss: %.4f\" % (i, w.item(), b.item(), loss.item()))\n",
    "    loss.backward()\n",
    "    \n",
    "    w.data -= w.grad * lr\n",
    "    b.data -= b.grad * lr\n",
    "    \n",
    "    w.grad.data.zero_()\n",
    "    b.grad.data.zero_()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 0,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
